package com.example.lt.controller;

import com.example.lt.constant.MessageConstants;
import com.example.lt.entity.Captcha;
import com.example.lt.entity.dto.FindPWDTO;
import com.example.lt.entity.dto.LoginDTO;
import com.example.lt.entity.dto.RegisterDTO;
import com.example.lt.entity.user.User;
import com.example.lt.entity.vo.LoginVO;
import com.example.lt.service.LoginService;
import com.example.lt.util.JwtUtil;
import com.example.lt.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 小涛Tao
 * @Date: 2024/07/27/16:22
 * version 1.0 注释：登录控制层，有 【注册、登录、找回密码、获取图形验证、获取邮箱验证码、检查验证码】 等接口
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/joyfulMoments/login")
public class LoginController {

    private final LoginService loginService;

    /**
     * 用户注册接口
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated RegisterDTO registerDTO) {
        log.info("用户注册：{}", registerDTO.getEmail());
        if (!loginService.register(registerDTO)) {
            return Result.error().message(MessageConstants.REGISTER_FAIL);
        }
        return Result.ok().message(MessageConstants.REGISTER_SUCCESS);
    }

    /**
     * 登录
     * @param loginDTO
     * @return
     */
    @PostMapping
    public Result login(@RequestBody @Validated LoginDTO loginDTO) {
        log.info("用户登录：{}", loginDTO.getEmail());
        User user = loginService.login(loginDTO);
        // 生成token
        String token = JwtUtil.getJwtToken(user.getId(), user.getNickName());
        // 返回登录结果
        LoginVO loginVO = LoginVO.builder().name(user.getNickName()).sex(user.getSex()).token(token).build();
        return Result.ok().data(loginVO);
    }

    /**
     * 找回密码接口
     * @param findPWDTO
     * @return
     */
    @PostMapping("/findPassword")
    public Result findPassword(@RequestBody @Validated FindPWDTO findPWDTO) {
        log.info("用户找回密码：{}", findPWDTO.getEmail());
        loginService.findPassword(findPWDTO);
        return Result.ok().message(MessageConstants.UPDATE_SUCCESS);
    }

    /**
     * 获取图形验证码，直接将图片以二进制的格式写入 response 来返回
     * @param response
     * @param uuid
     */
    @GetMapping("/captcha/{uuid}")
    public void getCaptcha(HttpServletResponse response, @PathVariable String uuid) throws IOException {
        log.info("获取图形验证码：{}", uuid);
        loginService.Captcha(response, uuid);
    }

    /**
     * 用户获取邮箱验证码
     * @param captcha
     * @return
     */
    @PostMapping("/getCode")
    public Result getCode(@RequestBody @Validated Captcha captcha) {
        log.info("用户获取邮箱验证码：{}", captcha.getEmail());
        if (!loginService.getCode(captcha)){
            Result.error().message(MessageConstants.CODE_ERROR);
        }
        return Result.ok().message(MessageConstants.SEND_SUCCESS);
    }

    /**
     * 检查邮箱验证码
     * @param email
     * @param code
     * @return
     */
    @PostMapping("/check")
    public Result check(String email, Integer code) {
        log.info("检查邮箱验证码：{} , {}", email, code);
        if (ObjectUtils.isEmpty(code)) return Result.error().message(MessageConstants.CODE_ISNULL);
        String codeStr = code.toString();
        loginService.checkCode(email, codeStr);
        return Result.ok().message(MessageConstants.CHECK_SUCCESS);
    }

}
